package com.question.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.achievement.dao.SubjectRepository;
import com.achievement.entity.Subject;
import com.common.util.TablePage;
import com.paper.dao.SubtopicsRepository;
import com.question.dao.JudgmentRepository;
import com.question.dao.OptionsRepository;
import com.question.entity.Judgment;
import com.question.entity.Multiple;
import com.question.entity.Options;
import com.question.entity.dto.JudgmentDTO;

@Service
@Transactional
public class JudgmentService implements IJudgmentService {
	@Autowired
	private JudgmentRepository judgDao;
	@Autowired
	private SubjectRepository subjectDao;
	@Autowired
	private OptionsRepository optionsDao;
	@Autowired
	private SubtopicsRepository subtopicsDao;

	public void saveOrUpdate(Judgment judgment, Long subjectId,String answerContent) {
		System.out.println(answerContent);
		Subject subject = subjectDao.findOne(subjectId);
		
		Options theTrue = new Options();
		String trueCode = UUID.randomUUID().toString();
		theTrue.setNum(trueCode);
		theTrue.setContent("true");
		optionsDao.save(theTrue);
		
		Options theFalse = new Options();
		String falseCode = UUID.randomUUID().toString();
		theFalse.setNum(falseCode);
		theFalse.setContent("false");
		optionsDao.save(theFalse);
		
		Options trueO = optionsDao.findByCode(trueCode);
		Options falseO = optionsDao.findByCode(falseCode);
		
		Set<Options> options = new HashSet<Options>();
		options.add(trueO);
		options.add(falseO);
		
		judgment.setSubject(subject);
		judgment.setOptions(options);
		if("true".equals(answerContent))
			judgment.setAnswer(trueO);
		else
			judgment.setAnswer(falseO);
		judgDao.save(judgment);
	}

	public void delete(Long id) {
		Judgment judgment = judgDao.findOne(id);
		judgment.setSubject(null);
		judgDao.save(judgment);
		judgDao.delete(id);
	}

	public TablePage<JudgmentDTO> findPage(Specification<Judgment> spec, Pageable pageable) {
		Page<Judgment> page = judgDao.findAll(spec, pageable);
		List<JudgmentDTO> list = new ArrayList<JudgmentDTO>();
		for(Judgment j:page.getContent()) {
			String content = "";
			if(j.getAnswer()!=null)content = j.getAnswer().getContent();
			list.add(new JudgmentDTO(j, j.getSubject().getName(),content));
		}
		return TablePage.getTablePage(new PageImpl<JudgmentDTO>(list, pageable, page.getTotalElements()));
	}

	public TablePage<Judgment> findRealPage(Specification<Judgment> spec, Pageable pageable) {
		
		return TablePage.getTablePage(judgDao.findAll(spec, pageable));
	}

	@Override
	public Judgment findOne(Long id) {
		// TODO Auto-generated method stub
		return judgDao.findOne(id);
	}

	@Override
	public boolean isNumExist(Judgment judgment) {
		// TODO Auto-generated method stub
		Judgment s = judgDao.findByNum(judgment.getNum());
		if(null!=s&&s.getId()!=judgment.getId())return true;
		return false;
	}

	@Override
	public boolean isUsing(Long id) {
		// TODO Auto-generated method stub
		if(subtopicsDao.findByJudgmentId(id).size()>0)
			return true;
		return false;
	}
}
